package com.REPDPMSystem.Utils;

import com.REPDPMSystem.Entity.Monitor;
import com.REPDPMSystem.Mapper.MonitorMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

@Component
public class ScreenCaptureUtil {
    @Autowired
    public  MonitorMapper monitorMapper;
    public  void captureScreen(String staff_id) {
        Monitor monitor = new Monitor();
        try {
            // 创建 Robot 实例
            Robot robot = new Robot();

            // 获取屏幕尺寸
            Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
            // 截取屏幕图像
            BufferedImage image = robot.createScreenCapture(screenRect);
            long now = System.currentTimeMillis();
            String filepath = "C:\\Users\\deathwing475\\Desktop\\REPDPMSystem\\assets\\screenshot\\"+"screenshot_" + now + ".png";
            monitor.setFilepath(filepath);
            monitor.setDate(now);
            monitor.setStaffId(staff_id);

            monitorMapper.insert(monitor);

            // 保存截图到文件
            File output = new File(filepath);
            ImageIO.write(image, "png", output);

            System.out.println("截图已保存到：" + output.getAbsolutePath());
        } catch (AWTException | IOException e) {
            e.printStackTrace();
        }
    }


}
